package com.reader.cms.controller.sys;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.sys.SysCommDetail;
import com.reader.cms.entity.sys.SysCommMaster;
import com.reader.cms.mapper.sys.SysCommDetailMapper;
import com.reader.cms.service.sys.ISysCommDetailService;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jiahaikun
 * @description 功能描述
 * @date 2018-08-06
 */
@RestController
@RequestMapping("/api/syscommdetail")
public class SysCommDetailController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(SysCommDetailController.class);

    @Autowired
    public ISysCommDetailService iSysCommDetailService;

    @Autowired
    SysCommDetailMapper sysCommDetailMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param param       其它查询条件，根据情况动态添加
     * @return
     * @function 分页查询数据
     */
    @GetMapping("getPageList")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "param") String param) {
        ResultMessageInfo resultMessage = null;
        Page<Map<String, Object>> list = iSysCommDetailService.selectListPage(currentPage, size, "");
        if (list.getTotal() > 0) {
            resultMessage = ResultMessageInfo.ofSuccess("成功", list);
        } else {
            return ResultMessageInfo.ofMessage("查不到数据！");
        }

        return resultMessage;
    }

    /**
     * @return
     * @function 自定义查询公共小类数据集
     */
    @GetMapping("list")
    public ResultMessageInfo getDetail(@RequestParam(value = "masterKey") String masterKey) {
        ResultMessageInfo resultMessage = null;
        List<SysCommDetail> commDetails = iSysCommDetailService.getDetail(masterKey);
        return ResultMessageInfo.ofSuccess("查询成功！", commDetails);
    }

    /**
     * @param sysCommDetail 实体对象
     * @return
     * @function 添加新记录
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody SysCommDetail sysCommDetail) {
        ValidatorUtil.validateEntity(sysCommDetail, AddGroup.class);//非空验证
        EntityWrapper<SysCommDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("code_key", sysCommDetail.getCodeKey());
        Integer detailCount = sysCommDetailMapper.selectCount(wrapper);
        if (detailCount != 0) {
            return ResultMessageInfo.ofMessage("公共小类编码已存在！！！");
        }
        Integer userId = this.getUserId();
        sysCommDetail.setDelFlag(0);
        sysCommDetail.setInsertDt(new Date());
        sysCommDetail.setInsertUserId(userId);
        sysCommDetail.setUpdtUserId(userId);
        ValidatorUtil.validateEntity(sysCommDetail, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage = null;
        boolean resultValue = iSysCommDetailService.insert(sysCommDetail);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("添加失败！");
        }

        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @param sysCommDetail 实体对象
     * @return
     * @function 修改
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody SysCommDetail sysCommDetail) {
        sysCommDetail.setUpdtDt(new Date());
        sysCommDetail.setUpdtUserId(this.getUserId());
        ValidatorUtil.validateEntity(sysCommDetail, AddGroup.class);//非空验证
        EntityWrapper<SysCommDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("code_key",
                sysCommDetail.getCodeKey()).ne("id", sysCommDetail.getId());
        Integer detailCount = sysCommDetailMapper.selectCount(wrapper);
        if (detailCount != 0) {
            return ResultMessageInfo.ofMessage("公共小类编码已存在！！！");
        }
        sysCommDetail.setDelFlag(0);
        boolean resultValue = iSysCommDetailService.updateById(sysCommDetail);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");
        }

        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @param sysCommDetail 实体对象
     * @return
     * @function 删除
     */
    @PostMapping("delete")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo delete(@RequestBody SysCommDetail sysCommDetail) {
        sysCommDetail.setUpdtDt(new Date());
        sysCommDetail.setDelFlag(1);
        sysCommDetail.setUpdtUserId(this.getUserId());
        ResultMessageInfo resultMessage = null;
        boolean resultValue = iSysCommDetailService.updateById(sysCommDetail);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("删除失败！");
        }

        return ResultMessageInfo.ofSuccess("删除成功！");
    }


    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除对象
     */
    @PostMapping("deleteIds")
    @Transactional
    @ApiOperation(value = "跟据ID删除记录", notes = "键为ids值为整型数组的json格式")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "id列表", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo deleteBatchIds(@RequestBody String data) {
        ResultMessageInfo resultMessage = null;
        List<Long> ids = JSON.parseArray(JSON.parseObject(data).getString("ids"), Long.class);
        boolean resultValue = iSysCommDetailService.deleteBatchIds(ids);
        if (!resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess("删除成功!", resultValue);
        } else {
            return ResultMessageInfo.ofMessage("删除失败！");
        }
        return resultMessage;

    }
}